
#include <asm.h>

.global C_SYMBOL(CurveBilinearWMMX_8u_C4A)

C_SYMBOL(CurveBilinearWMMX_8u_C4A):

InnerWidth= -0x50
srcy_16= -0x4C
Dst_SX16= -0x48
aSampleX16= -0x44
SrcYD16= -0x40
SrcXD16= -0x3C
DstPitch= -0x38
DstHeight= -0x34
DstWidth= -0x30
DstPt= -0x2C
SrcPitch = -0x1C
SrcHeight = -0x18
SrcWidth= -0x14
SrcPt= -0x10

    STMFD	SP!, {R4-R12,LR}
    SUB     SP, SP, #0x60
Start:
//.long 0xe7fddefe
    LDR     R4, [R0,#0x0]
    STR     R4, [SP,#0x60+SrcPt]

    LDR     R11, [R0,#0x8]
    STR     R11, [SP,#0x60+SrcPitch]

    LDR     R6, [R0,#0xC]
    STR     R6, [SP,#0x60+DstPitch]

    LDR     R4, [R0,#0x10]
    STR     R4, [SP,#0x60+SrcWidth]

    LDR     R4, [R0,#0x14]
    STR     R4, [SP,#0x60+SrcHeight]

    LDR     R10, [R0,#0x18]
    CMP	    R10, #0
    BLS	    Done
    STR     R10, [SP,#0x60+DstWidth]

    LDR     R4, [R0,#0x1C]
    CMP	    R4, #0
    BLS	    Done
    STR     R4, [SP,#0x60+DstHeight]

    LDR     R9, [R0,#0x2C]
    ADD     R9, R9, R4, LSL #2
    STR     R9, [SP,#0x60+aSampleX16]

    LDR     R9, [R0,#0x4]
    MLA     R9, R4, R6, R9
    STR     R9, [SP,#0x60+DstPt]

    LDR     R7, [R0,#0x20]
    STR     R7, [SP,#0x60+SrcXD16]

    LDR     R3, [R0,#0x24]
    STR     R3, [SP,#0x60+SrcYD16]

    LDR     R4, [R0,#0x28]
    STR     R4, [SP,#0x60+Dst_SX16]


    LDR     R4, [R0,#0x30]
    STR     R4, [SP,#0x60+InnerWidth]


    LDR	R1, =0x00000008
    TMCRR   wR15 , R1, R1

    LDR	R1, =0x00000000
    TMCRR   wR14 , R1, R1

    MOV	R3, #0
    STR	R3, [SP,#0x60+srcy_16]

    LDR LR, =0xFFFF

Header:

    LDR	R1, [SP,#0x60+srcy_16]
    MOV	R3, R1,	LSL#16
    MOV R8, R3, LSR#24

    MOV	R2, R1,LSR#16

    LDR R3, [SP,#0x60+SrcPt]
    MLA	R4, R2,	R11, R3

    LDR R3, [SP,#0x60+DstHeight]
    MUL R1, R3, R6
    SUB R9, R9, R1

    LDR     R12, [SP,#0x60+SrcWidth]
    LDR     R3, [SP,#0x60+Dst_SX16]
//.long 0xe7fddefe

    AND  R3, R3, LR
    SUB  R3, LR, R3
    MUL  R0, R12, R3
    MOV     R1, R10
    BL ___udivsi3

    MOV  R12, R0

    MOV R5, R7, LSR #14

    LDR	R2, [SP,#0x60+DstHeight]

LoopX:

    LDR	R1, [SP,#0x60+aSampleX16]

    SUB R1, R1, R2, LSL #2
    LDR R3, [R1]
    STR	R3, [SP,#0x60+Dst_SX16]

    MOV R3, R3, LSR #16
    CMP R3, #0
    BLE StartLoop

LoopHead:

    WSTRD wR14, [R9], #4
    SUBS R3, R3, #1
    BGT LoopHead


StartLoop:

    LDR LR, =0xFFFF
    LDR	R10, [SP,#0x60+InnerWidth]
    LDR R12, [SP,#0x60+SrcWidth]
    LDR R3, [SP,#0x60+Dst_SX16]

    AND  R3, R3, LR
    SUB  R3, LR, R3
    MUL  R0, R12, R3
    MOV     R1, R10

    BL ___udivsi3

    MOV  R12, R0
//.long 0xe7fddefe

InnerLoop:
    MOV	R1, R12, LSL#16
    MOV	R1, R1, LSR#24        // R1 = u8 , R8 = v8

    MOV	    R2, R12,LSR#16
    ADD	    R2, R4, R2, LSL #2

    WLDRW wR3, [R2]
    WLDRW wR1, [R2, #4]
    ADD	R3, R2,	R11
    WLDRW wR2, [R3]
    WLDRW wR0, [R3, #4]

 //   ADD R2, R2, R5, LSL #3
 //   PLD	[R2]
 //   PLD	[R2,R11]

    WUNPCKELUB wR2, wR2
    WUNPCKELUB wR0, wR0
    WUNPCKELUB wR3, wR3
    WUNPCKELUB wR1, wR1

    TBCSTH  wR5, R1
    TBCSTH  wR6, R8

    WSUBH  wR0, wR0, wR2
    WMULUL wR0, wR0, wR5

    WSUBH  wR1, wR1, wR3
    WMULUL wR1, wR1, wR5

    WSLLH   wR2, wR2, wR15
    WSLLH   wR3, wR3, wR15

    WADDH   wR0, wR0, wR2
    WADDH   wR1, wR1, wR3

    WSRLH   wR0, wR0, wR15
    WSRLH   wR1, wR1, wR15

    WSUBH   wR0, wR0, wR1
    WMULUL  wR0, wR0, wR6
    WSLLH   wR1, wR1, wR15
    WADDH   wR0, wR0, wR1
    WSRLH   wR0, wR0, wR15
    WPACKHUS wR0, wR0, wR1

    ADD R12, R12, R7

    SUBS R10, R10, #1

    WSTRW	wR0, [R9], #4

    BGT	InnerLoop

    LDR	R3, [SP,#0x60+Dst_SX16]
    LDR	R1, [SP,#0x60+InnerWidth]
    LDR	R2, [SP,#0x60+DstWidth]
    MOV R3, R3, LSR #16
    SUB R2, R2, R1
    SUBS R3, R2, R3
    BLE NextLine

LoopTail:

    WSTRD wR14, [R9], #4
    SUBS R3, R3, #1
    BGT LoopTail


NextLine:
//.long 0xe7fddefe
    LDR	R1, [SP,#0x60+srcy_16]
    LDR	R2, [SP,#0x60+SrcYD16]
    ADD	R1, R1,	R2
    STR	R1, [SP,#0x60+srcy_16]

    MOV	R3, R1, LSL#16
    MOV R8, R3, LSR#24

    MOV	R2, R1,LSR#16

    LDR R3, [SP,#0x60+SrcPt]
    MLA	R4, R2,	R11, R3


    LDR	R2, [SP,#0x60+DstHeight]
    SUBS R2, R2, #1

    LDR R9, [SP,#0x60+DstPt]
    MUL R1, R2, R6
    SUB R9, R9, R1

    STR	R2, [SP,#0x60+DstHeight]
    BGT  LoopX

Done:
    ADD	SP, SP,	#0x60
    LDMFD	SP!, {R4-R12,PC}

